﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CodeReason.Reports;
using System.IO;
using System.Windows.Threading;
using System.Data;

using System.Windows.Xps.Packaging;
using OpenExpressApp.WPF.Controls;
using Ricciolo.Controls;

namespace OpenExpressApp.Module.WPF.Command.WpfReport
{
    /// <summary>
    /// ReportWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ReportWindow : Window
    {
        /// <summary>
        /// 判断是否为第一次激活
        /// </summary>
        private bool _firstActivated = true;
        /// <summary>
        /// 生成报表所需的view
        /// </summary>
        private ListObjectView _view;

        public ReportWindow(ListObjectView view)
        {
            InitializeComponent();

            this._view = view;
        }

        /// <summary>
        /// 窗体激活事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Activated(object sender, EventArgs e)
        {
            if (this._firstActivated == false) return;
            this._firstActivated = false;

            WpfReportGen gen = new WpfReportGen();
            LoadReportData load = new LoadReportData(this._view.BOType.Name);
            object control = this._view.Control;
            this.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
            {
                try
                {
                    //生成模板
                    gen.GenerateTemplate(this._view);
                    //读取模板
                    ReportDocument reportDocument = new ReportDocument();
                    StreamReader reader = new StreamReader(new FileStream(System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, @"Templates\SimpleReport.xaml"), FileMode.Open, FileAccess.Read));
                    reportDocument.XamlData = reader.ReadToEnd();
                   // reportDocument.XamlImagePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Templates\");
                    reader.Close();
                    //装载数据
                    ReportData data = new ReportData();
                    data.ReportDocumentValues.Add("打印日期", DateTime.Now);
                    data.DataTables.Add(load.LoadData(control));
                    DateTime dateTimeStart = DateTime.Now;
                    XpsDocument xps = reportDocument.CreateXpsDocument(data);
                    documentViewer.Document = xps.GetFixedDocumentSequence();
                    Title = "打印预览 ———— 生成用时: " + (DateTime.Now - dateTimeStart);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    busyDecorator.IsBusyIndicatorHidden = true;
                }
            }));
        }
    }
}